WordPress has an API when working with custom meta tables but you have to follow the rules. We’ll cover what has to be done for those functions to work with your custom meta tables.
In the previous tutorial on working with custom Tables in WordPress, we’ve learned how to install the tables. In that tutorial we’ve introduced 2 tables, and we will use the same to cover the meta table API.
What’s important here is that we have a meta table named {$wpdb->prefix}myplugin_productmeta
and the unique column name myplugin_product_id
. That column name will behave as the column name post_id
inside of postmeta
table.
Registering the Meta Table
To work with WordPress meta table API, we need to register those table names inside of the global $wpdb
. In the previous tutorial, we did that. Here is the code:
The most important part when registering and creating the meta tables is to define the meta_type. Meta type here is myplugin_product
. So we use that when:
- registering table in $wpdb with
meta
suffix -> $wpdb->myplugin_productmeta - using the column name in meta table -> myplugin_product_id
What are the WordPress API for Meta Tables functions
Usually, when we work, we are using the functions that are wrapping the API for meta tables. Such functions are:
get_post_meta
update_post_meta
add_post_meta
delete_post_meta
Are those familiar? So, if you check the get_post_meta
function, you will see this:
function get_post_meta( $post_id, $key = '', $single = false ) {
return get_metadata( 'post', $post_id, $key, $single );
}
We will have to create our own functions for our custom meta tables as well. And we will wrap these ones:
Each of these functions accept the $meta_type
and $object_id
. The meta type in get_post_meta
is post and the object_id is a post ID.
How does the WordPress API for Meta Tables work
Now that we know all the functions that we need to pay attention to, let’s now learn how they actually work.
Each of the above-mentioned functions are using the private function _get_meta_table. This function requires the $meta_type
to be passed. So, when such meta type is passed, for example, myplugin_product
, it will check for a table name myplugin_productmeta
inside of the $wpdb
.
That’s why we had to register our custom table in $wpdb as $wpdb->myplugin_productmeta
.
Once the table was found, they’ll create the $column
by combining the meta type and '_id'
:
$column = sanitize_key( $meta_type . '_id' );
That’s why we had to have the myplugin_product_id
column in our custom meta table.
Creating your own Meta API functions
Now that you have the meta table registered and prepared, you can create your own function that will wrap the API. We’ll create four functions:
- get_myplugin_product_meta,
- update_myplugin_product_meta,
- add_myplugin_product_meta,
- delete_myplugin_product_meta.
These names do not have to be the same, but I’ll follow the naming convention. You can name them however you want since you’re just wrapping the existing API.
Getting the Meta data
function get_myplugin_product_meta( $id, $meta_key, $single = true ) {
return get_metadata( 'myplugin_product', $id, $meta_key, $single );
}
Updating the Meta data
function update_myplugin_product_meta( $id, $meta_key, $value ='' ) {
return update_metadata( 'myplugin_product', $id, $meta_key, $value );
}
Add the Meta data
function add_myplugin_product_meta( $id, $meta_key, $value ='' ) {
return add_metadata( 'myplugin_product', $id, $meta_key, $value );
}
Delete the Meta data
function delete_myplugin_product_meta( $id, $meta_key = '' ) {
return delete_metadata( 'myplugin_product', $id, $meta_key );
}
Now you have your own functions that work with your custom meta table.
Conclusion
WordPress custom meta tables are a great way for storing additional data since you don’t have to write everything yourself. You just need to create a table following the naming conventions, register it correctly and wrap the WordPress API. That’s it. Pretty simple and really useful.
But this could be a cumbersome thing to do with every new table (in case you add more than 1 or 2), right? No worries, we’ll create our own framework to work with that.
Have you created your own custom meta tables in WordPress? Share your thoughts on it below in the comments.
Become a Sponsor
Thank you for the article. I noticed that you are adding an sspackage_id index for the uncreated column. Or am I misunderstanding?
Hi Alexander, that was a mistake, it should have been the myplugin_product_id. Fixed it. Thanks for pointing out.
thanks a lot very nice post very helpfull to introduce me for managingand optimize db request!